package io.github.yotta.dict.server.converter;

import com.fasterxml.jackson.core.type.TypeReference;
import io.github.yotta.common.utils.jackson.JacksonUtil;
import io.github.yotta.dict.api.param.SysDictAddParam;
import io.github.yotta.dict.api.param.SysDictQueryParam;
import io.github.yotta.dict.api.param.SysDictUpdateParam;
import io.github.yotta.dict.api.vo.SysDictStatusVo;
import io.github.yotta.dict.api.vo.SysDictVo;
import io.github.yotta.dict.dao.entity.SysDict;
import io.github.yotta.dict.server.dto.SysDictStatusDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.Map;

/**
* @author yotta-generator
* @date 2025-08-11 10:12:20
*/
@Mapper
public interface  SysDictConverter  {

    SysDictConverter INSTANCE = Mappers.getMapper(SysDictConverter.class);;
    @Mappings(
            {
                    @Mapping(target = "extra", source = "extra", qualifiedByName = "map2Extra")
            }
    )
    SysDict param2Entity(SysDictAddParam addParam);

    @Mappings(
            {
                    @Mapping(target = "extra", source = "extra", qualifiedByName = "map2Extra")
            }
    )
    SysDict param2Entity(SysDictUpdateParam updateParam);



    @Mappings(
            {
                    @Mapping(target = "extra", source = "extra", qualifiedByName = "extra2Map")
            }
    )
    SysDictVo entity2Vo(SysDict entity);

    /**
     * 这个转换方法，不知为何mapstruct没有生成，所以这里手写
     */
     SysDictStatusVo convert(SysDictStatusDto sysDictStatusDto);



//    /**
//     * 这个转换方法，不知为何mapstruct没有生成，所以这里手写
//     */
//    default SysDictStatusVo convert(SysDictStatusDto sysDictStatusDto) {
//        SysDictStatusVo sysDictStatusVo = new SysDictStatusVo();
//        sysDictStatusVo.setCode(sysDictStatusDto.getCode());
//        sysDictStatusVo.setMessage(sysDictStatusDto.getMessage());
//        return sysDictStatusVo;
//
//    }

    SysDict param2Entity(SysDictQueryParam queryParam);



    @Named("extra2Map")
    default Map<String, String> extra2Map(String extra) {
        return extra == null || extra.isEmpty() ? null : JacksonUtil.parse(extra, new TypeReference<Map<String, String>>() {
        });
    }

    @Named("map2Extra")
    default String map2Extra(Map<String, String> extra) {
        return extra == null ? null : JacksonUtil.toJsonString(extra);
    }
}
